
<template>
    <van-address-edit 
        show-delete
        show-set-default
        :area-list="areaList"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="address"
        @save="save"
        @delete="del"
    />
</template>

<script setup>
    //引入地区数据
    import { areaList } from '@vant/area-data'
import { onMounted } from 'vue';

    let address = ref({
        name: '张三',
        tel: '13512666443',
        areaCode: '',
    })
    const {proxy} = getCurrentInstance()
    let business = proxy.$business.value
    let addressid = proxy.$route.query.addressid ? proxy.$route.query.addressid : 0

    onMounted(() => {
        AddressInfo()
    })

    const AddressInfo = async () => {
        var params = {
            addressid:addressid,
            busid:business.id
        }
        // console.log(params)
        var result = await proxy.$POST({
            url: 'Address/info',
            params,
        })
        // console.log(result)

        address.value = {
            name:result.data.consignee,
            tel:result.data.mobile,
            areaCode:result.data.district || result.data.city || result.data.province,
            addressDetail:result.data.address,
            isDefault: result.data.status == 1? true : false
        }
    }

    const save = async (values) => {
        // console.log(values)
        var params = {
            busid:business.id,
            name: values.name,
            mobile: values.tel,
            code:values.areaCode,
            address:values.addressDetail,
            status: values.isDefault ? true : false,
            adsid:addressid,
        }

        var result = await proxy.$POST({
            url:'address/edit',
            params: params
        })

        if(result.code == 0)
        {
            proxy.$fail(result.msg)
            return false
        }

        proxy.$success({
            message: result.msg,
            onClose: () => {
                proxy.$router.push({name: 'address'})
            }
        })

        console.log(result)

    }

    const del = async () => {
        proxy.$confirm ({
            title: '删除提醒',
            message: '是否要删除地址',
        }).then(async () => {
            var params = {
                busid: business.id,
                adsid: addressid,
            }

            var result = await proxy.$POST({
                url: 'address/del',
                params,
            })

            if(result.code == 0)
            {
                proxy.$fail(result.msg)
                return false
            }else
            {
                proxy.$success({
                    message: result.msg,
                    onClose: () => {
                        proxy.$router.push({name: 'address'})
                    }
                })
            }
        }).catch(() => console.log('取消'))
    }

</script>